package com.weetop.aklyscan.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.base.BaseActivity;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.LogisticsBean;
import com.weetop.aklyscan.presenter.SendOutPresent;
import com.weetop.aklyscan.view.SendOutView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LogisticsActivity extends BaseActivity<SendOutPresent> implements SendOutView {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.status)
    TextView status;
    @BindView(R.id.webView)
    WebView webView;
    private CommonRecyclerAdapter<LogisticsBean.DateBean> adapter;
    private List<LogisticsBean.DateBean> list = new ArrayList<>();
    private String express_sn, type, order_sn, express_code;

    @Override
    protected SendOutPresent createPresenter() {
        return new SendOutPresent(this, this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logistics);
        ButterKnife.bind(this);
        express_sn = getIntent().getStringExtra("express_sn");
        type = getIntent().getStringExtra("type");
        order_sn = getIntent().getStringExtra("order_sn");
        express_code = getIntent().getStringExtra("express_code");
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new CommonRecyclerAdapter<LogisticsBean.DateBean>(mActivity, R.layout.item_logistics, list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, LogisticsBean.DateBean item, int position) {
                helper.setText(R.id.time, item.getTime());
                helper.setText(R.id.content, item.getContext());
                if (list.size() - 1 == position) {
                    helper.setVisible(R.id.line1, View.VISIBLE);
                    helper.setVisible(R.id.line2, View.INVISIBLE);
                } else if (position == 0) {
                    helper.setVisible(R.id.line1, View.INVISIBLE);
                    helper.setVisible(R.id.line2, View.VISIBLE);
                } else {
                    helper.setVisible(R.id.line1, View.VISIBLE);
                    helper.setVisible(R.id.line2, View.VISIBLE);
                }
            }
        };
        recyclerView.setAdapter(adapter);
        mPresenter.expressInfo(express_sn, type, order_sn, express_code);
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void getexpressInfoSuccess(BaseModel<LogisticsBean> o) {
        LogisticsBean logisticsBean = o.getData();
        for (int i = 0; i < logisticsBean.getData().size(); i++) {
            list.add(logisticsBean.getData().get(i));
        }
        adapter.addAll(list);
        if (list.size() > 0) {
            status.setText(logisticsBean.getData().get(0).getStatus());
        }
        webView.loadUrl(logisticsBean.getTrailUrl());
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        webSettings.setSupportZoom(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setDisplayZoomControls(true);
        webSettings.setDefaultFontSize(12);

        webSettings.setAllowFileAccess(true);
    }
}
